package cn.impl;

import cn.annotation.Log;
import cn.basic.HibBaseDAOImpl;
import cn.basic.iHibBaseDAO;
import cn.common.properties.LogOperType;
import cn.dao.SystemModelDAO;
import cn.model.TRoleSystemModel;
import cn.model.TSystemModel;
import cn.model.VRoleSystemModel;
import org.springframework.stereotype.Component;

import java.util.List;


@Component("systemmodeldao")
public  class SystemModelDAOImpl implements SystemModelDAO {

    private iHibBaseDAO bdao = null;

    public SystemModelDAOImpl(){
        bdao = new HibBaseDAOImpl();
    }

    @Log(isSaveLog=true,operationType= LogOperType.QUERY,operationName="查询表获取菜单列表")
    @Override
    public List<TSystemModel> getTSystemModelList() {
        String hql = "from TSystemModel order by parentid,displayorder asc";
        return bdao.select(hql);
    }

    @Log(isSaveLog=true,operationType= LogOperType.QUERY,operationName="查询视图获取菜单列表")
    @Override
    public List<VRoleSystemModel> getAllTSystemModel(String wherecondition,
                                                     int currentPage, int pageSize) {
        String hql = "from VRoleSystemModel";
        if (wherecondition == null && wherecondition.equals("")){
            hql += "order by user desc";
        }
        if (wherecondition != null && !wherecondition.equals("")){
            hql += wherecondition;
        }
        List<VRoleSystemModel> list = bdao.selectByPage(hql, currentPage, pageSize);
        return list;
    }

    @Log(isSaveLog=true,operationType= LogOperType.QUERY,operationName="查询视图获取菜单数量")
    @Override
    public int getAllTSystemModelCount(String wherecondition) {
        String hql = "select count(*) from VRoleSystemModel";
        if (wherecondition != null && !wherecondition.equals("")) {
            hql += wherecondition;
        }
        return bdao.selectValue(hql);
    }

    @Log(isSaveLog=true,operationType= LogOperType.QUERY,operationName="查询菜单表所有数据")
    @Override
    public List<TSystemModel> getAllTSystemModelByTab(String wherecondition, int currentPage, int pageSize) {
        String hql = "from TSystemModel ";
        if (wherecondition != null && !wherecondition.equals("")){
            hql += wherecondition;
        }
        if (wherecondition == null && wherecondition.equals("")){
            hql += "order by sysid desc";
        }
        List<TSystemModel> list = bdao.selectByPage(hql, currentPage, pageSize);
        return list;
    }

    @Log(isSaveLog=true,operationType= LogOperType.QUERY,operationName="查询菜单表的菜单数量")
    @Override
    public int getAllTSystemModelCountByTab(String wherecondition) {
        String hql = "select count(*) from TSystemModel";
        if (wherecondition != null && !wherecondition.equals("")) {
            hql += wherecondition;
        }
        return bdao.selectValue(hql);
    }

    @Log(isSaveLog=true,operationType= LogOperType.QUERY,operationName="查询一级菜单表所有数据")
    @Override
    public List<TSystemModel> getAllTSystemModelone(String wherecondition,
                                                    int currentPage, int pageSize) {
        String hql = "from TSystemModel ";
        if (wherecondition == null && wherecondition.equals("")){
            hql += "order by sysid desc";
        }
        if (wherecondition != null && !wherecondition.equals("")){
            hql += wherecondition + "and deepth = 1 ";
        }
        List<TSystemModel> list = bdao.selectByPage(hql, currentPage, pageSize);
        return list;
    }

    @Log(isSaveLog=true,operationType= LogOperType.QUERY,operationName="查询一级菜单项列表")
    @Override
    public List<TSystemModel> getTSystemModelListByOne() {
        String hql = "from TSystemModel where deepth = 1 order by parentid,displayorder asc";
        return bdao.select(hql);
    }

    @Log(isSaveLog=true,operationType= LogOperType.QUERY,operationName="通过sysid查询一级菜单项列表")
    @Override
    public List<TSystemModel> getTSystemModelListOneBySysId(int sysid) {
        String hql = "from TSystemModel where deepth = 1 and sysid=?";
        Object[] param = {sysid};
        return bdao.select(hql,param);
    }

    @Log(isSaveLog=true,operationType= LogOperType.QUERY,operationName="查询1级菜单下的2级菜单项列表")
    @Override
    public List<TSystemModel> getTSystemModelListByOne(int oldid) {
        String hql = "from TSystemModel where deepth = 2 and parentid = ? order by parentid,displayorder asc";
        Object[] param = {oldid};
        return bdao.select(hql,param);
    }

    @Log(isSaveLog=true,operationType= LogOperType.QUERY,operationName="按照角色选择获取对应的菜单项列表")
    @Override
    public List<VRoleSystemModel> getSystemModelByRole(int roleid) {
        String hql = "from VRoleSystemModel where Roleid = ? and isedit = 1 order by parentid,displayorder asc ";
        Object[] param = {roleid};
        return bdao.select(hql, param);
    }

    @Log(isSaveLog=true,operationType= LogOperType.QUERY,operationName="授权部分，通过roleid查询当前角色拥有哪些菜单权限")
    @Override
    public List<VRoleSystemModel> getSystemModelByRoleid(int roleid,int currentPage, int pageSize) {
        String hql = "from VRoleSystemModel where roleid = ? order by parentid,displayorder asc ";
        Object[] param = {roleid};
        return bdao.select(hql, param);
    }

    @Log(isSaveLog=true,operationType= LogOperType.QUERY,operationName="授权部分，通过roleid查询当前角色下菜单数量")
    @Override
    public int getSystemModelAmountByRoleid(int roleid){
        String hql = "select count(*) from VRoleSystemModel where roleid = ?";
        Object[] param = {roleid};
        return bdao.selectValue(hql,param);
    }

    @Log(isSaveLog=true,operationType= LogOperType.QUERY,operationName="获取网站所有的一级菜单项列表")
    @Override
    public List<TSystemModel> getTSystemModelListByPage(int currentPage, int pageSize) {
        String hql = "from TSystemModel where deepth = 1 order by parentid,displayorder asc";
        return bdao.selectByPage(hql, currentPage, pageSize);
    }

    @Log(isSaveLog=true,operationType= LogOperType.QUERY,operationName="获取指定1级菜单下的2级菜单项列表")
    @Override
    public List<TSystemModel> getTSystemModelListByPageTwo(int oldid, int currentPage, int pageSize) {
        String hql = "from TSystemModel where deepth = 2 and parentid = ? order by parentid,displayorder asc";
        Object[] param = {oldid};
        return bdao.selectByPage(hql,param,currentPage, pageSize);
    }

    @Log(isSaveLog=true,operationType= LogOperType.ADD,operationName="添加系统菜单")
    @Override
    public int addSystemModel(TSystemModel SystemModel) {
        return (Integer) bdao.insert(SystemModel);
    }

    @Log(isSaveLog=true,operationType= LogOperType.MODIFY,operationName="修改系统菜单")
    @Override
    public boolean modifySystemModel(TSystemModel tSystemModel) {
        return bdao.update(tSystemModel);
    }

    @Log(isSaveLog=true,operationType= LogOperType.QUERY,operationName="查询二级菜单项列表")
    @Override
    public TSystemModel getTSystemModelById(int id) {
        return (TSystemModel) bdao.findById(TSystemModel.class, id);
    }

    @Log(isSaveLog=true,operationType= LogOperType.QUERY,operationName="判断二级菜单是否已有")
    @Override
    public boolean isTSystemModelListByid(int oldid) {
        String hql = "from TSystemModel where deepth = 2 and sysid = ? order by parentid,displayorder asc";
        Object[] param = {oldid};
        List<TSystemModel> list = bdao.select(hql,param);
        if(list.size()==0){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 先查询角色菜单表是否已经有数据
     * @return
     */
    @Log(isSaveLog=true,operationType= LogOperType.QUERY,operationName="先查询角色菜单表是否已经有数据")
    @Override
    public List<TRoleSystemModel> getTSystemModelByTid(int id) {
        String hql = "from TRoleSystemModel where id= ?";
        Object[] param = {id};
        return bdao.select(hql,param);
    }

    @Log(isSaveLog=true,operationType= LogOperType.QUERY,operationName="查询当前角色当前菜单是否已经授予")
    @Override
    public List<VRoleSystemModel> getIsSysModelByRole(int sysid, int roleid) {
        String hql = "from VRoleSystemModel where sysid = ? and  Roleid= ?";
        Object[] param = {sysid,roleid};
        return bdao.select(hql,param);
    }

    @Log(isSaveLog=true,operationType= LogOperType.QUERY,operationName="通过ID查询菜单")
    @Override
    public VRoleSystemModel getIsSysModelById(int id) {
        return (VRoleSystemModel) bdao.findById(VRoleSystemModel.class, id);
    }

    @Log(isSaveLog=true,operationType= LogOperType.ADD,operationName="添加菜单信息")
    @Override
    public int addRoleSystemModel(TRoleSystemModel obj) {
        return (Integer) bdao.insert(obj);
    }


    @Log(isSaveLog=true,operationType= LogOperType.MODIFY,operationName="修改角色系统菜单")
    @Override
    public boolean modifyRoleSystemModel(TRoleSystemModel rolemodel) {
        return bdao.update(rolemodel);
    }


    @Log(isSaveLog=true,operationType= LogOperType.DELETE,operationName="删除")
    @Override
    public boolean deletesystemmole(int sysid) {
        TSystemModel TSystemModel = (cn.model.TSystemModel) bdao.findById(cn.model.TSystemModel.class, sysid);
        return bdao.delete(TSystemModel);
    }

    @Log(isSaveLog=true,operationType= LogOperType.QUERY,operationName="查询角色菜单表是否已经存在该数据")
    @Override
    public List<TRoleSystemModel> getTRoleSystemModelById(int roleid, int sysid){
        String hql = "from TRoleSystemModel where roleid = ? and sysid = ?";
        Object[] param = {roleid,sysid};
        return bdao.select(hql,param);
    }
}
